package com.gtenacg.cartoonservice.service.impl;

import com.gtenacg.cartoonservice.common.Const;
import com.gtenacg.cartoonservice.compent.MyRestTemplate;
import com.gtenacg.cartoonservice.compent.WindowsExplorerStringComparator;
import com.gtenacg.cartoonservice.mapper.CartoonMapper;
import com.gtenacg.cartoonservice.pojo.model.*;
import com.gtenacg.cartoonservice.pojo.vo.CartoonVo;
import com.gtenacg.cartoonservice.service.CartoonService;
import lombok.RequiredArgsConstructor;
import org.apache.ibatis.exceptions.PersistenceException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author 86198
 * @description 针对表【cartoon(漫画)】的数据库操作Service实现
 * @createDate 2024-05-06 16:50:49
 */
@Service
@RequiredArgsConstructor
public class CartoonServiceImpl implements CartoonService {

    private final CartoonMapper cartoonMapper;

    @Override
    public List<Cartoon> list() {
        return cartoonMapper.list();
    }


    @Override
    public List<Cartoon> getByCids(List<Integer> cids) {
        if (cids == null || cids.isEmpty()) return null;
        return cartoonMapper.getByCids(cids);
    }

    @Override
    public List<String> getChapterList(Cartoon cartoon) {
        List<String> list = MyRestTemplate.FileServiceGetChapter(cartoon.getSrc(), false);
        if (list == null) return null;
        list.sort(new WindowsExplorerStringComparator());
        return list;
    }

    @Override
    public List<String> getChapterInfos(String src) {
        List<String> infos = MyRestTemplate.FileServiceGetChapter(src, true);
        infos.sort(new WindowsExplorerStringComparator());
        infos = infos.stream().map(info -> src + info).toList();
        return infos;
    }

    @Override
    public Integer insert(Cartoon cartoon) throws DuplicateKeyException {
        cartoon.setSrc(Const.FILE_PATH + Const.CARTOON_PATH + cartoon.getName() + "/");
        cartoon.setCover("");
        cartoonMapper.insert(cartoon);
        return cartoon.getCid();
    }


    @Override
    public List<CartoonVo> assembleCartoonVo(List<Cartoon> cartoons,
                                             List<CartoonType> types, List<RelationCartoonType> relationTypes,
                                             List<Author> authors, List<RelationCartoonAuthor> relationAuthors) {
        // 使用Map存储CartoonType，以便快速查找
        // 使用ctid作为键，使用本体对象作为值
        Map<Integer, CartoonType> typeMap = types.stream()
                .collect(Collectors.toMap(CartoonType::getCtid, Function.identity()));


        // 使用Map存储RelationCartoonType
        // 使用cid作为键，使用ctid的集合作为值
        Map<Integer, List<Integer>> relationTypesMap = relationTypes.stream()
                .collect(Collectors.groupingBy(
                        RelationCartoonType::getCid,
                        Collectors.mapping(RelationCartoonType::getCtid, Collectors.toList())
                ));


        // 使用Map存储Author，以便快速查找
        // 使用aid作为键，使用本体对象作为值
        Map<Integer, Author> authorMap = authors.stream()
                .collect(Collectors.toMap(Author::getAid, Function.identity()));


        // 使用Map存储RelationCartoonAuthor，以便快速查找
        // 使用cid作为键，使用aid作为值
        Map<Integer, List<Integer>> relationAuthorsMap = relationAuthors.stream()
                .collect(Collectors.groupingBy(
                        RelationCartoonAuthor::getCid,
                        Collectors.mapping(RelationCartoonAuthor::getAid, Collectors.toList())
                ));


        // 数据拼装 List<CartoonVo> List
        List<CartoonVo> list = cartoons.stream()
                // 通过cid,获取对应的cartoonVO对象
                .map(cartoon -> {
                    Integer cid = cartoon.getCid();

                    //通过cid,获取对应typeName列表
                    List<Integer> ctidList = relationTypesMap.getOrDefault(cid, Collections.emptyList());
                    List<String> typeNames = ctidList.stream()
                            .map(ctid -> typeMap.getOrDefault(ctid, new CartoonType()).getName()).toList();

                    //通过cid,获取对应authorName列表
                    List<Integer> aidList = relationAuthorsMap.getOrDefault(cid, Collections.emptyList());
                    List<String> authorNames = aidList.stream()
                            .map(aid -> authorMap.getOrDefault(aid, new Author()).getName()).toList();

                    return new CartoonVo(cartoon, typeNames, authorNames);
                })
                .toList();


        return list;
    }

    @Override
    public void update(Cartoon cartoon) {
        cartoonMapper.update(cartoon);
    }

    @Override
    public void delete(List<Integer> cids) {
        cartoonMapper.delete(cids);
    }

    @Override
    public List<Cartoon> search(String name) {
        return cartoonMapper.search(name);
    }

    @Override
    public List<Cartoon> random(Integer num) {
        return cartoonMapper.random(num);
    }
}




